package com.topofstack.flp.ui;

import java.io.File;

import com.bappi.custom.activities.CustomActivity;
import com.topofstack.flp.FLPApplication;
import com.topofstack.flp.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NoteListActivity extends CustomActivity  implements OnItemClickListener {
	
	ListView listView;
	FLPApplication application;
	String []notes;
	File []files;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_note_list);
		
		application = (FLPApplication)getApplication();
		
		listView = (ListView) findViewById(R.id.mylist);
		listView.setOnItemClickListener(this);
	}
	
	@Override
	protected void onStart() {
		super.onStart();

		files = application.getAssetManager().getAllNotes();
		if(files.length > 0){
			notes = new String[files.length];
			for(int i=0; i<files.length; i++){
				String name = files[i].getName();
				String []partz = name.split("___");
				notes[i] = partz[0];
			}
			
//			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//					  android.R.layout.simple_list_item_1, android.R.id.text1, notes);
			
			listView.setAdapter( new NoteListAdapter(this));
		}
	}
	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent i = new Intent(this, NoteComposeActivity.class);
		i.putExtra("name",files[position].getName());
//		startActivity(i);
		pushIntnet(i);
	}
	
	public class NoteListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private Context context;

		public NoteListAdapter(Context context) {
			this.context = context;
			mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return notes.length;
		}

		public Object getItem(int position) {
			return notes[position];
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {
					convertView = mInflater.inflate(
							R.layout.layout_note_list_content, null);
					holder = new ViewHolder();
					holder.textView = (TextView) convertView
							.findViewById(R.id.text_view_title);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				holder.textView.setText(notes[position]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			TextView textView;
		}
	}
}
